#include "stdafx.h"
#include "LoginUserInfo.h"
#include "BaseInclude.h"

LoginUserInfo * LoginUserInfo::sysRecords = NULL;

LoginUserInfo::LoginUserInfo(QObject *parent)
	:QObject(parent)
{

}

LoginUserInfo::~LoginUserInfo()
{

}

LoginUserInfo * LoginUserInfo::getInstance()
{
	if (sysRecords)
		return sysRecords;

	sysRecords = new LoginUserInfo;
	return sysRecords;
}

bool LoginUserInfo::initLoginUserInfo(QString userid, QString username)
{
	userOrganInfo.strUserCode = userid;
	userOrganInfo.strUserName = username;
	if (readUserInfo() == false)
	{
		clearLoginUserInfo();
		return false;
	}

	return true;
}

void LoginUserInfo::clearLoginUserInfo()
{
	userOrganInfo.strUserName = "";
	userOrganInfo.strUserID = "";
	userOrganInfo.strUserCode = "";
	userOrganInfo.strDeptName = "";
	userOrganInfo.strDeptID = "";
	userOrganInfo.strCompanyName = "";
	userOrganInfo.strCompanyID = "";
}

bool LoginUserInfo::readUserInfo()
{
	QRecord dsRecord;
	QString sql = QString("SELECT S.CORPORATION_ID COMPANYID,\
		(SELECT O.ORGAN_NAME FROM V_STRU_ORGAN O WHERE O.STRU_ID = S.CORPORATION_ID) COMPANYNAME,\
		S.DEPARTMENT_ID DEPTID,\
		(SELECT O.ORGAN_NAME FROM V_STRU_ORGAN O WHERE O.STRU_ID = S.DEPARTMENT_ID) DEPTNAME,\
		(SELECT O.ORGAN_ID FROM V_STRU_ORGAN O WHERE O.STRU_ID = S.EMPLOYEE_ID) USERID\
		FROM PUB_USERS S\
		WHERE S.USER_ID = '%1'").arg(userOrganInfo.strUserCode);
	dsRecord.select(sql);
	CHK_EXP_RET(dsRecord.rowCount()!=1,false);
	userOrganInfo.strCompanyID = dsRecord.getCollectStr(0, "COMPANYID");
	userOrganInfo.strCompanyName = dsRecord.getCollectStr(0, "COMPANYNAME");
	userOrganInfo.strDeptID = dsRecord.getCollectStr(0, "DEPTID");
	userOrganInfo.strDeptName = dsRecord.getCollectStr(0, "DEPTNAME");
	userOrganInfo.strUserID = dsRecord.getCollectStr(0, "USERID");
	return true;
}

//======================================================================================================================================

QString LoginUserInfo::getUserName()
{
	return userOrganInfo.strUserName;
}

QString LoginUserInfo::getUserId()
{
	return userOrganInfo.strUserID;
}

QString LoginUserInfo::getDeptName()
{
	return userOrganInfo.strDeptName;
}

QString LoginUserInfo::getDeptId()
{
	return userOrganInfo.strDeptID;
}

QString LoginUserInfo::getPasswd()
{
	return userOrganInfo.strPasswd;
}

QString LoginUserInfo::getCompanyId()
{
	return userOrganInfo.strCompanyID;
}

QString LoginUserInfo::getCompanyName()
{
	return userOrganInfo.strCompanyName;
}

UserOrganInfo LoginUserInfo::getUserOrganInfo()
{
	return userOrganInfo;
}
